package com.ruoyi.web.core.config;

import org.springframework.web.socket.*;

public class MyWebSocketHandler implements WebSocketHandler {

    //建立新的 socket 连接后回调的方法
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        System.out.println("新连接");
        //从session中获取存放的参数
        session.getAttributes().get("id");
    }

    // 接收客户端发送的 Socket
    @Override
    public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
        // 连接发送的消息
        String msg = message.getPayload().toString();
        // 通过session向连接发送消息
        session.sendMessage(new TextMessage("发送消息"));
    }

    //连接出错时，回调的方法
    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
        System.out.println("收到的错误信息：" + exception);
    }


    //连接关闭时，回调的方法
    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
        System.out.println("断开连接");
    }


    // WebSocketHandler 是否处理部分消息 默认返回false即可
    @Override
    public boolean supportsPartialMessages() {
        return false;
    }

}
